package com.bush.mall.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

import com.bush.mall.common.entity.Result;
import com.bush.mall.common.exception.SaaSException;
import com.bush.mall.member.mapper.MemberMapper;
import com.bush.mall.member.entity.MemberEntity;
import com.bush.mall.member.service.MemberService;

/**
 * 会员
 * @author RUN
 * @date 2022-11-29
 */
@Service(MemberService.BEAN_ID)
@Slf4j
public class MemberServiceImpl extends ServiceImpl<MemberMapper, MemberEntity> implements MemberService {

    /**
     * 查询会员列表
     * @author RUN
     * @date 2022-11-29
     */
    @Override
    public Result queryForPage(Integer current,Integer size) {
        try{
            Assert.notNull(current,"参数异常");
            Assert.notNull(size,"参数异常");
            Page<MemberEntity> page = new Page<>(current,size);
            QueryWrapper<MemberEntity> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("create_time");
            baseMapper.selectPage(page,wrapper);
            return Result.ok(page);
        }catch(Exception e){
            log.error("MemberService queryForPage ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 创建会员表单对象
     * @author RUN
     * @date 2022-11-29
     */
    @Override
    public Result saveMember(MemberEntity member) {
        try{
            Assert.notNull(member,"参数异常");
            boolean flag = this.save(member);
            if (flag) return Result.ok("会员添加成功");
            throw new SaaSException("会员添加失败");
        }catch(Exception e){
            log.error("MemberService saveMember ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }


    /**
     * 修改会员表单对象
     * @author RUN
     * @date 2022-11-29
     */
    @Override
    public Result updateMember(MemberEntity member) {
        try{
            Assert.notNull(member,"参数异常");
            boolean flag = this.updateById(member);
            if (flag) return Result.ok("会员修改成功");
            throw new SaaSException("会员修改失败");
        }catch(Exception e){
            log.error("MemberService updateMember ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }


    /**
     * 通过id删除会员表单
     * @author RUN
     * @date 2022-11-29
     */
    @Override
    public Result deleteMember(Long id) {
        try{
            Assert.notNull(id,"参数异常");
            boolean flag = this.removeById(id);
            if (flag) return Result.ok("会员删除成功");
            throw new SaaSException("会员删除失败");
        }catch(Exception e){
            log.error("MemberService deleteMember ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }




}